﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Data;
namespace TierService
{
    public class XMLHelper : Object
    {
        protected static string strXmlFile;

        protected static XmlDocument objXmlDoc = new XmlDocument();

        public XMLHelper(string XmlFile, bool bOverWrite, string sRoot)
        {
            try
            {
                //如果覆盖模式，则强行创建一个xml文档
                if (bOverWrite)
                {
                    objXmlDoc.AppendChild(objXmlDoc.CreateXmlDeclaration("1.0", "gb2312", null));//设置xml的版本，格式信息
                    objXmlDoc.AppendChild(objXmlDoc.CreateElement("", sRoot, ""));//创建根元素
                    objXmlDoc.Save(XmlFile);//保存
                }
                else //否则，检查文件是否存在，不存在则创建
                {
                    if (!(File.Exists(XmlFile)))
                    {
                        objXmlDoc.AppendChild(objXmlDoc.CreateXmlDeclaration("1.0", "gb2312", null));
                        objXmlDoc.AppendChild(objXmlDoc.CreateElement("", sRoot, ""));
                        objXmlDoc.Save(XmlFile);
                    }
                }
                objXmlDoc.Load(XmlFile);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            strXmlFile = XmlFile;
        }
        /// <summary>
        /// 查找数据。返回一个DataTable
        /// </summary>
        /// <param name="XmlPathNode"></param>
        /// <returns></returns>
        public static DataTable GetData(string XmlPathNode)
        {
            //查找数据。返回一个DataView
            DataSet ds = new DataSet();
            try
            {
                StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
                ds.ReadXml(read);
                return ds.Tables[0];
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 更新节点內容,如果多个相同的节点,则更新第一个节点
        /// </summary>
        /// <param name="xmlPathNode"></param>
        /// <param name="content"></param>
        public static void UpdateNode(string xmlPathNode, string content)
        {
            objXmlDoc.SelectSingleNode(xmlPathNode).InnerText = content;
        }
        /// <summary>
        /// 更新节点,带属性和内容
        /// </summary>
        /// <param name="xmlPathNode"></param>
        /// <param name="arrAttribName"></param>
        /// <param name="arrAttribContent"></param>
        /// <param name="content"></param>
        public static void UpdateNode(string xmlPathNode, string[] arrAttribName, string[] arrAttribContent, string content)
        {
            //修改内容
            objXmlDoc.SelectSingleNode(xmlPathNode).InnerText = content;

            //修改属性
            for (int i = 0; i <= arrAttribName.GetUpperBound(0); i++)
            {
                objXmlDoc.SelectSingleNode(xmlPathNode).Attributes[i].Value = arrAttribContent[i];
            }
        }
        /// <summary>
        /// 刪除一个节点。
        /// </summary>
        /// <param name="Node"></param>
        public static bool DeleteNode(string Node)
        {
            try
            {
                string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
                objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
            }
            catch
            {
                //throw;   
                return false;
            }
            return true;
        }
        /// <summary>
        /// 插入一节点和此节点的一子节点。
        /// </summary>
        /// <param name="mainNode"></param>
        /// <param name="ChildNode"></param>
        /// <param name="Element"></param>
        /// <param name="Content"></param>
        public static void InsertNode(string mainNode, string ChildNode, string Element, string Content)
        {
            XmlNode objRootNode = objXmlDoc.SelectSingleNode(mainNode);
            XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
            objRootNode.AppendChild(objChildNode);//插入节点
            XmlElement objElement = objXmlDoc.CreateElement(Element);
            objElement.InnerText = Content;
            objChildNode.AppendChild(objElement);//插入子节点
        }
        /// <summary>
        /// 插入一个节点，带一属性。
        /// </summary>
        /// <param name="mainNode"></param>
        /// <param name="Element"></param>
        /// <param name="Attrib"></param>
        /// <param name="AttribContent"></param>
        /// <param name="Content"></param>
        public static void InsertNode(string mainNode, string Element, string Attrib, string AttribContent, string Content)
        {
            XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
            XmlElement objElement = objXmlDoc.CreateElement(Element);
            objElement.SetAttribute(Attrib, AttribContent);
            objElement.InnerText = Content;
            objNode.AppendChild(objElement);
        }
        /// <summary>
        /// 插入一个节点，不带属性。
        /// </summary>
        /// <param name="mainNode"></param>
        /// <param name="Element"></param>
        /// <param name="Content"></param>
        public static void InsertNode(string mainNode, string Element, string Content)
        {
            XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
            XmlElement objElement = objXmlDoc.CreateElement(Element);
            objElement.InnerText = Content;
            objNode.AppendChild(objElement);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mainNode"></param>
        /// <param name="elementName"></param>
        /// <param name="arrAttributeName"></param>
        /// <param name="arrAttributeContent"></param>
        /// <param name="elementContent"></param>
        public static void InsertNode(string mainNode, string elementName, string[] arrAttributeName, string[] arrAttributeContent, string elementContent)
        {
            XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
            XmlElement objElement = objXmlDoc.CreateElement(elementName);
            for (int i = 0; i <= arrAttributeName.GetUpperBound(0); i++)
            {
                objElement.SetAttribute(arrAttributeName[i], arrAttributeContent[i]);
            }
            objElement.InnerText = elementContent;
            objNode.AppendChild(objElement);
        }
        /// <summary>
        /// 保存文档。
        /// </summary>
        public static void Save()
        {
            try
            {
                objXmlDoc.Save(strXmlFile);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            objXmlDoc = null;
        }
    }
}
